<?php

namespace Frieza\Modules\Position\Controllers;

use Doraemon\model\Position\PositionExtend;
use Doraemon\model\Position\PositionStartApp;
use Doraemon\service\position\AdmExtOperationModule;
use Doraemon\service\position\PositionConstInterface;
use Doraemon\service\position\PositionModule;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class CarouselBannerWithPosController extends BaseController
{
    use BaseTrait;

    public function saveModule(Request $request, $baseInfoId)
    {
        $base = $request->input('base', []);
        $rules = $request->input('rules', []);
        $pic = $base['pic_id'] ?? '';
        $pos = $request->input('base.pos', 0);

        switch ($this->positionId) {
            case PositionConstInterface::CONTRACTOR_CAROUSEL_BANNER:
            case PositionConstInterface::USER_DETAIL_CAROUSEL_BANNER:
            case PositionConstInterface::HOME_CAROUSEL_V5:
                if ($pos <= 0)  throw new \Exception('轮播图位置必选大于0');
                if ($this->base_field_pos && ($this->base_field_pos != $pos)) throw new \Exception('轮播图位置不允许变更');
                $ext_type = $request->input("base.ext_type", "");
                if (empty($pic)) throw new \Exception('图片必须上传');
                $id = $base['id'] ?? 0;
                $copy_id = $base['copy_id'] ?? 0;
                $img_size = $rules['img_size'] ?? 0;
                if (!(($id > 0 || $copy_id > 0) && $img_size == 0)) $this->rulesValid($rules, 0);
                break;
            case 65:
                $key_word = $request->input('key_word', '');
                PositionExtend::saveSearchResultBanner($baseInfoId, $key_word);
                break;
            default:
                break;
        }
    }


    public function defaultBaseInfo(Request $request)
    {
        $response = parent::defaultBaseInfo($request);

        $response['pos'] = 1;

        return $response;
    }

    public function beforeLinkForm(Form $form)
    {
        switch ($this->positionId) {
            case 122:
                $form->text('base.title', '专题名称');
                $form->select('base.ext_type', '所属分类')->options($this->experience_type);
                $this->bannerFormWithCheck($form, 'base.pic_id', '封面图');
                break;
            case PositionConstInterface::HOME_CAROUSEL_V5:
                $form->radio('base.ext_type', '独占')->options([0 => '否', 1 => '是'])->help('独占模式默认仅限商业广告使用，请提前至占位系统进行占位及内部同步');
                $options = \Doraemon\service\position\apiModule\CarouselBannerWithPos::$map;
                $form->radioButton('base.pos', '轮播图位置')
                    ->options($options)
                    ->help("前一个位置空缺时,后面的位置会补齐<br/>没有其它配置时兜底配置生效");
                $this->bannerFormWithCheck($form);
                break;
            case 65:
                $form->radioButton('base.pos', '轮播图位置')
                    ->options(array_combine(range(1, 5), range(1, 5)));
                $this->bannerFormWithCheck($form);
                $form->text('key_word', '跳转链接')->required();
                break;
            case PositionConstInterface::TAB5_CREATIVE_ACTIVITY_CHANNEL:
                $form->text('base.title', '标题');
                $form->radioButton('base.pos', '轮播图位置')
                    ->options(array_combine(range(1, 5), range(1, 5)));
                $this->bannerFormWithCheck($form);
                break;
            case PositionConstInterface::HOME_CHANNEL_V6:
                $form->radioButton('base.pos', '位置')->options(array_combine(range(1, 20), range(1, 20)));
                $form->text('base.title', '标题');
                $form->imgUpload('base.pic_id', '图标');
                $form->imgUpload('base.single_pic_id', '角标')->help("角标旨在让用户及时发现频道有了新变动
<br/>
但出于对app整体的考虑，为避免「多个角标同时出现造成视觉混乱，降低角标对用户吸引度」的情况，同一时间段内，仅允许一个角标生效
<br/>
如果在选择生效时间时，发现某一时段不可选，是正常现象");
                $this->gifControl($form, 'base.play_times', '角标播放次数');
                break;
            case 118:
                $form->radioButton('base.pos', '位置')
                    ->options(array_combine(range(1, 4), range(1, 4)));
                $this->bannerFormWithCheck($form, 'base.pic_id', '图标');
                $form->text('base.title', '标题');
                break;
            case 119:
                $form->radioButton('base.pos', '位置')
                    ->options(array_combine(range(1, 12), range(1, 12)));
                $this->bannerFormWithCheck($form, 'base.pic_id', '图标');
                $form->text('base.title', '标题');
                break;
            case 121:
                $form->radioButton('base.pos', '轮播图位置')
                    ->options(array_combine(range(1, 3), range(1, 3)));
                $this->bannerFormWithCheck($form);
                break;
            default:
                $form->radioButton('base.pos', '轮播图位置')
                    ->options(array_combine(range(1, 5), range(1, 5)));
                $this->bannerFormWithCheck($form);
                break;
        }

    }
    private $experience_type = [
        '如何花钱', '搞定需求', '找对服务',
    ];
    public function extendList(Grid $grid)
    {
        switch ($this->positionId) {
            case 122:
                $grid->column('title', '专题名称');
                $grid->column('ext_type', '所属分类')->using($this->experience_type);
                $grid->column('pic_url', '专题封面图片')->image("", 120);
                return;
            case PositionConstInterface::HOME_CAROUSEL_V5:
                $grid->column('ext_type', '独占')->bool();
                break;
            case PositionConstInterface::HOME_CHANNEL_V6:
                $grid->column('pos', '位置')->label($this->labelColor(20));
                $grid->column('title', '标题');
                $grid->column('pic_url', '图标')->image('', 50);
                $grid->column('single_pic_url', '角标')->image('', 50);
                return;
            default:
                break;
        }
        $grid->column('pos_name', '位置')->label([
            1 => 'success',
            2 => 'warning',
            3 => 'success',
            4 => 'info',
            5 => 'default',
            "兜底1" => 'info',
            "兜底2" => 'success',
            "兜底3" => 'warning',
        ]);
        $grid->column('pic_url', '图片')->image("", 120);
        if ($this->positionId == 65) {
            $grid->column('ext->key_word', '链接')->width(400);
        }
        if ($this->positionId == PositionConstInterface::TAB5_CREATIVE_ACTIVITY_CHANNEL || in_array($this->positionId, [118, 119])) {
            $grid->column('title', '标题');
        }
    }

}
